<?php

if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
    
$objXajax->registerFunction('fmfrmmonitoring_loadGrid');      
//$objXajax->registerFunction('fmfrmmonitoring_checkedRoom');
$objXajax->registerFunction('fmfrmmonitoring_loadRoom');
$objXajax->registerFunction('fmfrmmonitoring_LoadProduct');
$objXajax->registerFunction('fmfrmmonitoring_init');
//$objXajax->registerFunction('fmmonitoring_deleteproduct');

function fmfrmmonitoring_init(){
    global $objDbSelect,$lang,$arrMenu,$arrdetail,$hotel_id;
    $objResponse = new xajaxResponse();
    $objResponse->addScript("xajax_fmfrmmonitoring_loadGrid();");
    return $objResponse->getXML();
}

function fmfrmmonitoring_loadRoom(){ 
    global $lang,$arrDetail,$objDbSelect;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrDetail = fmfrmmonitoring_getRoomInit();
    $objResponse->addScript('fmfrmmonitoring_resetView();');        
     for($i=0;$i<count($arrDetail);$i++){
        $id = $arrDetail[$i]['id'];
        //$numperson = $arrDetail[$i]['numperson'];
        //$numglass= $arrDetail[$i]['num_glass'];
        $timerent= $arrDetail[$i]['timerent'];
        $current_id = $arrDetail[$i]['current_id']==''?0:$arrDetail[$i]['current_id'];
        $status = $timestamp>0?1:0;
        $objResponse->addScript("fmfrmmonitoring_AssignRoom($id,$current_id,'$timerent');");
    }
        
    $arr = fmfrmreportcurrent_summary();
    $arr[0]['timerent'] = ConvertTimeToHM($arr[0]['timerent']);
    $arr[0]['money_rent'] = $arr[0]['money_rent']+ $arr[0]['money_glass']+$arr[0]['money_person'];  ;
    $money_voucher = $arr[0]['money_voucher'];
    $money_total = $arr[0]['money_total'];
    $money_arising = $arr[0]['money_arising'];
    $money_rent = $arr[0]['money_rent'];
    $arr[0]['money_product'] = $money_total- $money_rent+$money_voucher+$money_arising;
    
    $money_get = 0;
    $money_pay =0;
    $to_time = date('H:i:s');
    $to_date = date('d/m/Y');
    
    $sqlpay = " select sum(money) as money,flg from cus_pay where datediff(date_created,now())=0 and service_id = $hotel_id GROUP  by flg ";

   $arrPay = $objDbSelect->GetArray($sqlpay);
    for($i=0;$i<count($arrPay);$i++){
        if($arrPay[$i]['flg']==1){
           $money_get = $arrPay[$i]['money']; 
        }else{
            $money_pay = $arrPay[$i]['money']; 
        }
    }
    $money_get=$money_get==''?0:$money_get;
    $money_pay=$money_pay==''?0:$money_pay;
    $money_revenue = $money_total - $money_pay + $money_get;
    $arr[0]['money_revenue']=$money_revenue;
    $arr[0]['money_pay']=$money_pay;
    $arr[0]['money_get']=$money_get;
    
        
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_monitoring_now.html');
    $oTbs->MergeBlock('blk_now', $arr); 
     $objResponse->addAssign('fmfrmmonitoring_divNow', 'innerHTML', $oTbs->Source);
    
       $objResponse->addScript('changeWaiting(1);'); 
        //$objResponse->addScript('fmAssignTime();');
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function fmfrmmonitoring_loadGrid(){     

    global $lang,$arrMenu,$arrDetail,$hotel_id,$objDbSelect;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrDetail = array();
    $arrRoom = fmfrmmonitoring_getRoom($arrDetail);
    
    $oTbs = new clsTinyButStrong();    
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_monitoring_grid1.html');   
    $oTbs->MergeBlock('blk_total', $arrRoom); 
    
    $oTbs->MergeBlock('blk_data', 'array', 'arrDetail[%p1%]'); 
    
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('fmfrmmonitoring_divGrid', 'innerHTML', $oTbs->Source);
    
    $objResponse->addScript("setTimeout('fmfrmmonitoring_firstload()',2000);"); 
   //$objResponse->addScriptCall("fmfrmmonitoring_checkundobtn();");        
        
             
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function  fmfrmmonitoring_getRoom(&$arrDetail){
    global $objDbSelect,$tbl_postfix,$hotel_id;
    $arrResult = array();
    $sqlSelect = "SELECT r.id,r.room_type,r.name,t.style as color,t.name  as name_type,t.id as type_id
    FROM fm_room as r join fm_room_type  as t on r.room_type=t.id and t.service_id = $hotel_id where r.active = 1 and t.active = 1
     order by r.room_type,r.name";
   // echo $sqlSelect; die();
    $arr =  $objDbSelect->GetArray($sqlSelect);
    for($i=0;$i<count($arr);$i++){
        $type_id = $arr[$i]['type_id'];
        $arrResult[$type_id]= array('type_id'=>$arr[$i]['type_id'],'name_type'=>$arr[$i]['name_type']);
        $arrDetail[$type_id][]=array('id'=>$arr[$i]['id'],'color'=>$arr[$i]['color'],'name'=>$arr[$i]['name']);
    }
    return $arrResult;
}
/*
*hÃ m nÃ y dÃ¹ng Ä‘á»ƒ láº¥y danh sÃ¡ch báº£ng lÃªn Ä‘á»ƒ lÆ°u vÃ o mÃ£ng 
* 
*/
function  fmfrmmonitoring_getRoomInit(){
    global $objDbSelect,$tbl_postfix,$hotel_id;
    $arrResult = array();
    $sqlSelect = "SELECT r.id,c.id as current_id,DATE_FORMAT(c.timestart,'%Hh%i') as timerent
    FROM fm_current_book$tbl_postfix as c join fm_room as r 
    on c.room_id = r.id and flgcal=0 and c.flg_cancel = 0 and r.active = 1 ";
    $arr =  $objDbSelect->GetArray($sqlSelect);
    return $arr;
}
    
function fmfrmmonitoring_LoadProduct($thisId){
    global $objDbSelect,$tbl_postfix,$lang;
     $objResponse = new xajaxResponse();
     $arrId = split('_',$thisId);
    $roomId =addslashes($arrId[2]) ;
     //$service_type =  SVT_FILM;
    $sql = "select p.name,p.price,p.num,p.price*p.num as money from cus_history_product$tbl_postfix as p
    where general_id = (select id from fm_current_book$tbl_postfix where room_id = '$roomId' and flgcal = 0 and num>0 and flg_cancel = 0) order by p.date_created "; // lấy sản phẩm trên bàn từ DB
    $arr = $objDbSelect->GetArray($sql);
    if(is_array($arr)&&count($arr)>0){
        $oTbs = new clsTinyButStrong();    
        $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_monitoringproduct_grid.html');   
        $oTbs->MergeBlock('blk_data', $arr); 
        $oTbs->Show(TBS_NOTHING);
        $objResponse->addAssign('fmfrmmonitoring_divProduct', 'innerHTML', $oTbs->Source);
        $objResponse->addScript("$('#fmfrmmonitoring_divProduct').dialog('open')");        
    }else{
        $objResponse->addAlert("Phòng này không có sản phẩm!");
    }
    

  
   return $objResponse->getXML();  
}

    
?>
